

from rest_framework.generics import  ListCreateAPIView

from rest_framework.permissions import IsAdminUser


from meiduo_admin.serializers.user import UserListSerializer, UserAddSerializer
from meiduo_admin.utils import PageNum, UserPermission
from users.models import User





class UserView(ListCreateAPIView):
    #serializer_class = UserListSerializer
    pagination_class = PageNum
    permission_classes = [UserPermission]



    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword is None or keyword == '':
            # 获取全部用户数据
           return User.objects.all()
        # 获取关键字查询的数据
        return User.objects.filter(username__contains=keyword)

    def get_serializer_class(self):
        # 返回指定的序列化器
        if self.request.method == 'POST':
            return UserAddSerializer

        return UserListSerializer

